package chuangyi.com.org.ViewModel;

import android.app.Activity;
import android.content.Context;
import android.databinding.Bindable;
import android.databinding.ObservableArrayList;
import android.widget.Toast;

import com.solidfire.gson.internal.LinkedTreeMap;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import chuangyi.com.org.DOMIHome.BR;
import chuangyi.com.org.DOMIHome.LoadingDialog;
import chuangyi.com.org.DOMIHome.R;
import chuangyi.com.org.DOMIHome.Utils.Utils;
import chuangyi.com.org.DOMIHome.Utils.WebApi;
import chuangyi.com.org.DOMIHome.adapter.BindingListItem;
import chuangyi.com.org.DOMIHome.adapter.ListBindingAdapter;
import chuangyi.com.org.DOMIHome.adapter.SwipeListAdapter;
import chuangyi.com.org.DOMIHome.adapter.XListAdapterInterface;
import chuangyi.com.org.DOMIHome.adapter.XListBindingAdapter;

/**
 * 我的收藏
 * Created by zxh on 16/11/25.
 */
public class MyCollectModel extends XListModel {
    private boolean videoType;
    private boolean articleType;

    @Bindable
    public boolean isVideoType() {
        return videoType;
    }

    public void setVideoType(boolean videoType) {
        this.videoType = videoType;
        if (this.videoType) {
            setArticleType(false);
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
        notifyPropertyChanged(BR.videoType);
    }

    @Bindable
    public boolean isArticleType() {
        return articleType;
    }

    public void setArticleType(boolean articleType) {
        this.articleType = articleType;
        if (this.articleType) {
            setVideoType(false);
            getAdapter().clear(true);
            setCurrentPage(1);
            load(1);
        }
        notifyPropertyChanged(BR.articleType);
    }

    public MyCollectModel(Context context) {
        super(context);
        super.getHandler().setTitle("我的收藏");
//        setAdapter(new SwipeListAdapter(getContext(), 0));
        this.articleType = true;
        List<Integer> viewTypeList = new ArrayList<Integer>();
        viewTypeList.add(R.layout.i_collect_video);
        viewTypeList.add(R.layout.i_collect_article_item);
//        setAdapter(new SwipeListAdapter(getContext(), 0, viewTypeList));
        getAdapter().setViewType(viewTypeList);
    }

    @Override
    public void load(final int page) {
        final boolean isVideoType = videoType;
        new Thread(new Runnable() {
            @Override
            public void run() {

                if (isVideoType) {
                    loadVideo(getContext(), page);
                } else {
                    loadArticle(getContext(), page);
                }
            }
        }).start();
    }

    private void loadArticle(final Context context, final int page) {
        final ArrayList list = WebApi.getCollectedArticle(getContext(), page);
        final XListAdapterInterface adapter = this.getAdapter();
        adapter.setVariableId(BR.articleItem);
        try {
            for (Object message : list) {
                LinkedTreeMap map = (LinkedTreeMap) message;
                CollectArticleItem item = new CollectArticleItem(context, R.layout.i_collect_article_item);
                item.setTitle((String) map.get("title"));
                item.setContent((String) map.get("context"));
                item.setArticleId(Utils.getIntValue(map.get("articleid")));
                item.setBrowser(Utils.getIntValue(map.get("browser")));
                item.setCollectcoun(Utils.getIntValue(map.get("collectcoun")));
                item.setCollectId(Utils.getIntValue(map.get("collectid")));
                item.setCollectTime(Utils.getDateFromFormat(map.get("collecttime"), "yyyy/MM/dd HH:mm:ss"));
                adapter.add(item);
            }
        } catch (Exception ex) {
            showToast("解析数据时发生错误");
        }
        if (list.size() != 0) {
            setCurrentPage(page);
        }
        notifyDataSetChanged(list);
    }

    private void loadVideo(final Context context, final int page) {
        final ArrayList list = WebApi.getCollectedVideo(getContext(), page);
        final XListAdapterInterface adapter = this.getAdapter();
        adapter.setVariableId(BR.video);
        try {
            for (Object message : list) {
                LinkedTreeMap map = (LinkedTreeMap) message;
                CollectVideoItem item = new CollectVideoItem(context, R.layout.i_collect_video);
                item.setVideoname((String) map.get("videoname"));
                item.setVideodesc((String) map.get("videodesc"));
                item.setVideoid(Utils.getIntValue(map.get("videoid")));
                item.setBrowser(Utils.getIntValue(map.get("browser")));
                item.setCollectcoun(Utils.getIntValue(map.get("collectcoun")));
                item.setCollectid(Utils.getIntValue(map.get("collectid")));
                item.setCollecttime(Utils.getDateFromFormat(map.get("collecttime"), "yyyy/MM/dd HH:mm:ss"));
                adapter.add(item);
            }
        } catch (Exception ex) {
            showToast("解析数据时发生错误");
        }

        if (list.size() != 0) {
            setCurrentPage(page);
        }
        notifyDataSetChanged(list);
    }
}
